package com.gezi.admin.module.sys.dict.data;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gezi.admin.config.exception.type.ApiException;
import com.gezi.admin.module.sys.dict.data.model.AppDictDataAddParamModel;
import com.gezi.admin.module.sys.dict.data.model.AppDictDataEditParamModel;
import com.gezi.admin.module.sys.dict.data.model.AppDictDataSearchModel;
import com.gezi.admin.module.sys.dict.dict.AppDictEntity;
import com.gezi.admin.module.sys.dict.dict.AppDictService;
import com.gezi.admin.module.config.BaseController;
import com.gezi.admin.module.config.utils.PageUtil;
import com.gezi.admin.module.config.utils.PageVo;
import com.gezi.admin.security.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Author 格子软件
 * @createDate 2024/2/10 09:00
 * @contact 公众号：格子软件 微信：13716343106 邮箱：1424529270@qq.com
 */
@Api(tags = "字典数据管理")
@RestController
@RequestMapping("/api/app/dict/data")
public class AppDictDataController extends BaseController {

    @Resource
    private AppDictDataService appDictDataService;

    @Resource
    private AppDictService appDictService;

    @ApiOperation("查询分页")
    @GetMapping("/findAllByPage")
    public Object findAllByPage(PageVo page, AppDictDataSearchModel searchVo) {
        Page<Map> pageData = appDictDataService.findByPage(PageUtil.initMpPage(page),searchVo);
        for (Map record : pageData.getRecords()) {
            AppDictEntity appDictEntity = appDictService.getById(record.get("dictId").toString());
            if(appDictEntity != null){
                record.put("dictName",appDictEntity.getName());
            }else{
                record.put("dictName","");
            }
        }
        return pageData;
    }

    @ApiOperation("查询列表")
    @GetMapping("/findAllByList")
    public Object findAllByList(AppDictDataSearchModel searchVo) {
        String userId = SecurityUtil.getUserId();
        searchVo.setUserId(userId);
        List<Map> list = appDictDataService.findByList(searchVo);
        return list;
    }

    @ApiOperation("查询详情")
    @GetMapping("/findById/{id}")
    public Map findById(@PathVariable @NotBlank(message = "参数不能为空")String id) {
        AppDictDataEntity appDictDataEntity = appDictDataService.getById(id);
        if(appDictDataEntity == null){
            throw ApiException.getInstance("数据不存在，请刷新重试");
        }

        Map result = JSON.parseObject(JSON.toJSONString(appDictDataEntity),Map.class);

        return result;
    }

    @ApiOperation("添加")
    @PostMapping("/add")
    public void add(@Validated @RequestBody AppDictDataAddParamModel paramModel) {
        //校验字典
        AppDictEntity appDictEntity = appDictService.getById(paramModel.getDictId());
        if(appDictEntity == null){
            throw ApiException.getInstance("数据不存在，请刷新重试");
        }
        AppDictDataEntity dictIdAndType = appDictDataService.findByDictIdAndType(paramModel.getDictId(),paramModel.getType());
        if(dictIdAndType != null){
            throw ApiException.getInstance("标识已存在");
        }
        AppDictDataEntity appDictDataEntity = new AppDictDataEntity();
        appDictDataEntity.setData(paramModel);
        appDictDataService.save(appDictDataEntity);
    }

    @ApiOperation("修改")
    @PostMapping("/edit")
    public void edit(@Validated @RequestBody AppDictDataEditParamModel paramModel) {
        //校验参数
        AppDictDataEntity appDictDataEntity = appDictDataService.getById(paramModel.getId());
        if(appDictDataEntity == null){
            throw  new ApiException("数据不存在，请刷新重试");
        }
        AppDictDataEntity dictIdAndType = appDictDataService.findByDictIdAndType(paramModel.getDictId(),paramModel.getType());
        if(dictIdAndType != null && !dictIdAndType.getId().equals(paramModel.getId())){
            throw ApiException.getInstance("标识已存在");
        }
        appDictDataEntity.setData(paramModel);
        appDictDataService.saveOrUpdate(appDictDataEntity);
    }

    @ApiOperation("删除")
    @DeleteMapping("/delByIds/{ids}")
    public void delById(@PathVariable String[] ids) {
        appDictDataService.removeBatchByIds(Arrays.asList(ids));
    }
}